//
//  CPSCAll2CategoryRightRecommendCell.m
//  CollTest
//
//  Created by codew on 11/11/21.
//

#import "CPSCAll2CategoryRightRecommendCell.h"
#import "CPSCAll2CategoryRightICollectionCell.h"
#import "CPSCAll2CategoryVC_Constants.h"

@interface CPSCAll2CategoryRightRecommendCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (strong, nonatomic) IBOutlet UICollectionView * collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *collectionLayout;
@property (nonatomic, assign) NSInteger indexSelect;
@property (nonatomic, strong) NSMutableArray <id<CPSCAll2CategoryRightViewUIModelProtocol>>*  dataArray;

@end

static NSString * CellIdentifier = @"CPSCAll2CategoryRightICollectionCell";


@implementation CPSCAll2CategoryRightRecommendCell

+ (CGFloat)heightCellDataArrayCount:(NSInteger)count
{
    CGFloat height = 0;

    // 文字View高度 + UICollectionView高度
    NSInteger rowValue = count /  KValueColumnCPSCCRRCollection;
    NSInteger moValue = count % KValueColumnCPSCCRRCollection;
    rowValue = rowValue +  (moValue ? 1 : 0);
    
    CGFloat heightAllCell = rowValue * KValueHeightCPSCCRRCollection;
    height = + heightAllCell;
    
    return height;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

    self.collectionView.scrollEnabled = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    [self setupCollectionViewLayout];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setupCollectionViewLayout
{
    
    CGFloat margin = 0;
    CGFloat cellWidth = KValueWidthCPSCCRRCollection;
    // VS_EDBottomLabelView的高
    CGFloat cellHeight = KValueHeightCPSCCRRCollection;
    _collectionLayout.itemSize = CGSizeMake(cellWidth, cellHeight);
    _collectionLayout.minimumInteritemSpacing = margin;
    _collectionLayout.minimumLineSpacing = margin;
    
    _collectionLayout.sectionInset = UIEdgeInsetsMake(0, margin, 0, margin);
    [_collectionLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    // 注册cell
    [_collectionView registerNib:[UINib nibWithNibName:CellIdentifier bundle:nil]
      forCellWithReuseIdentifier:CellIdentifier];
}


- (void)updateCellWithItems:(NSMutableArray <id<CPSCAll2CategoryRightViewUIModelProtocol>>* )dataItems
{
    _dataArray = dataItems;
    [self.collectionView reloadData];
}


#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    id<CPSCAll2CategoryRightViewUIModelProtocol> item = _dataArray[indexPath.row];        
    
    if (self.blockSelectRecommend) {
        
        self.blockSelectRecommend(item);
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CPSCAll2CategoryRightICollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    id<CPSCAll2CategoryRightViewUIModelProtocol> item = _dataArray[indexPath.row];
    
    [cell updateCellWithItem:item];
    return cell;
}

@end
